const moment = require("moment");

module.exports = function(sequelize, DataTypes){
    var Role = sequelize.define('AdminRole', {
        role_id: {
            type: DataTypes.INTEGER,
            allowNull: false,
            autoIncrement: true,
            primaryKey: true
        },
        role_name: {
            type: DataTypes.STRING(10)
        },
        description: {
            type: DataTypes.STRING(100)
        },
        creator_id: {
            type: DataTypes.BIGINT(20),
            defaultValue: 0
        },
        created_at: {
            type: DataTypes.DATE,
            defaultValue: new Date(),
            get() {
                return moment(this.getDataValue('created_at')).format('YYYY-MM-DD HH:mm:ss');
            }
        },
        modified_id: {
            type: DataTypes.BIGINT(20),
            defaultValue: 0
        },
        modified_at: {
            type: DataTypes.DATE,
            defaultValue: new Date(),
            get() {
                return moment(this.getDataValue('modified_at')).format('YYYY-MM-DD HH:mm:ss');
            }
        },
        is_del: {
            type: DataTypes.TINYINT(1),
            defaultValue: 0
        }
    }, {
        timestamps: false,
        freezeTableName: true,
        tableName: 'system_role',
    });

    Role.schema("public");

    Role.associate = function (model) {
        addUsers(Role, model.AdminUser);
        addMenus(Role, model.AdminMenu);
    };

    Role.destroyRoles = async function(role_ids) {
        return sequelize.transaction().then(function (t) {
            this.t = t;
            return sequelize.query("UPDATE system_role SET is_del=1 WHERE role_id IN ("+role_ids.join(',')+")", {
                type: sequelize.QueryTypes.DELETE,
                transaction: this.t
            });
        }).then(function() {
            return sequelize.query("DELETE FROM system_menu_role WHERE role_id IN ("+role_ids.join(',')+");", {
                type: sequelize.QueryTypes.DELETE,
                transaction: this.t
            });
        }).then(function() {
            return sequelize.query("DELETE FROM system_user_role WHERE role_id IN ("+role_ids.join(',')+");", {
                type: sequelize.QueryTypes.DELETE,
                transaction: this.t
            });
        }).then(function() {
            return this.t.commit();
        }).catch(function (err) {
            return this.t.rollback();
        });
    };

    // 同步模型到数据库
    Role.sync({force: false}).then(function () {

    });

    return Role;
};

function addUsers(role, user) {
    role.belongsToMany(user, {
        as: 'Users',
        through: 'system_user_role',
        timestamps: false,
        foreignKey: {
            name: 'role_id', // 外键的字段名
            allowNull: false
        },
        otherKey: {
            name: 'user_id',
            allowNull: false
        }
    });
}

function addMenus(role, menu) {
    role.belongsToMany(menu, {
        as: 'Menus',
        through: 'system_menu_role',
        timestamps: false,
        foreignKey: {
            name: 'role_id', // 外键的字段名
            allowNull: false
        },
        otherKey: {
            name: 'menu_id',
            allowNull: false
        }
    });
}

